const Writable = require('stream').Writable; 
const util = require('util');

module.exports = class MyStream extends Writable {
    constructor(matchText, options) {
        super();
        this.count = 0;
        this.matcher = new RegExp(matchText, 'ig');
    }

    _write(chunk, encoding, cb) {
        let matches = chunk.toString().match(this.matcher);
        if (matches) {
            this.count += matches.length;
        }
        if (cb) {
            cb();
        }
    }

    end() {
        this.emit('total', this.count);
    }
}

/* let Writable = require('stream').Writable;
let util = require('util');

module.exports = MyStream;

util.inherits(MyStream, Writable);

function MyStream(matchText, options) {
    Writable.call(this, options);
    this.matchText = matchText;
    this.count = 0;
    this.matcher = new RegExp(matchText, 'ig');
}

MyStream.prototype._write = function (chunk, encoding, cb) {
    let matches = chunk.toString().match(this.matcher);
    if (matches) {
        this.count += matches.length;
    }
    cb();
}

MyStream.prototype.end = function () {
    this.emit('total', this.count);
}; */